import { ref, computed, watch } from 'vue'
import { useRouter } from 'vue-router'
import { Projects } from '../../core/project/project'
import { Toast } from 'vant';
export const useProject = (props?: any, initData?: any, emit?: any) => {
  const router = useRouter()
  

  //详情页面
  const details = async (project: Project) => {
    router.push({
      path: "/details",
      query: {
        policyNo: project.policyNo
      }
    });
  }


  let projectList = ref([] as Project[])
  const loading = ref(false)
  const finished = ref(false)
  let page = 1
  const pageNum = 5

  const onLand = () => {
    const getProjectList = async () => {
      const res = await Projects.find({ page: page, pageNum: pageNum })
      projectList.value.push(...res.data)
      loading.value = false
      if (projectList.value.length >= res.total) {
        finished.value = true
      }
    }
    getProjectList()
    page++
  }
  //筛选页面
  const projectTypes = ref([{ name: '股份保险公司', checked: false },
  { name: '相互保险公司', checked: false },
  { name: '专属保险公司', checked: false }])

  const companys = ref([{ name: '人寿保险公司', checked: false },
  { name: '平安保险公司', checked: false },
  { name: '阳光保险公司', checked: false },
  { name: '太平保险公司', checked: false },
  { name: '新华保险公司', checked: false }])

  const projectTypeCheck = (item: any) => {
    item.checked = !item.checked
  }
  const myScreen = ref(null) as any;
  const show = ref(false);
  const showPopup = () => {
    myScreen.value.show = true;
  };
  const value = ref('');
  const onSearch = (val: any) => Toast(val);
  const onCancel = () => Toast('取消');

  const date = ref('');
  const showcalendar = ref(false);
  const formatDate = (date: any) => `${date.getMonth() + 1}/${date.getDate()}`;
  const onConfirm = (values: any) => {
    const [start, end] = values;
    showcalendar.value = false;
    date.value = `${formatDate(start)} - ${formatDate(end)}`;
  };
  const defaultDate = ref(new Date())
  const reset = () => {
    projectTypes.value.forEach((item) => {
      item.checked = false
    })
    companys.value.forEach((item) => {
      item.checked = false
    })
    defaultDate.value = new Date()
  }
  const determine = () => {
    show.value = false;
  }
  return {
    details,
    onLand,
    loading,
    finished,
    projectList,
    projectTypes,
    companys,
    show,
    value,
    showcalendar,
    defaultDate,
    date,
    reset,
    determine,
    onConfirm,
    showPopup,
    onSearch,
    onCancel,
    projectTypeCheck,
    myScreen
  }
}
